home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Applications / Alpha.5.96 folder / Tcl / SystemCode / grep.tcl < prev    next >
Encoding:
Text File  |  1994-09-19  |  2.3 KB  |  93 lines  |  [TEXT/ALFA]

  1. #================================================================================
  2. # 'greplist' and 'grepfset' are used for batch searching from the "find" dialog.
  3. #  Hence, you really shouldn't mess with them unless you know what you are doing.
  4. #================================================================================
  5. proc greplist {args} {
  6.     set num [expr [llength $args] - 2]
  7.     set exp [lindex $args $num]
  8.     set arglist [lindex $args [expr $num + 1]]
  9.     
  10.     set opened 0
  11.     set owin 0
  12.     set cid [scancontext create]
  13.  
  14.     set cmd [lrange $args 0 [expr $num - 1]]
  15.     eval scanmatch $cmd {$cid $exp {
  16.             if (!$owin) {
  17.                 set owin 1
  18.                 new -n {* Batch Find *}
  19.  
  20.                 global winModes
  21.                 set name [lindex [winNames] 0]
  22.                 changeMode [set winModes($name) Brws]
  23.  
  24.                 insertText "(<cr> to go to match)\r-----\r"
  25.                 set w [lindex [winNames -f] 0]
  26.                 set opened 1
  27.             }
  28.             set l [expr 20 - [string length [file tail $f]]]
  29.             insertText -w $w "\"[file tail $f]\"[format "%$l\s" ""]; Line $matchInfo(linenum): $matchInfo(line)\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t∞$f\r"}
  30.     }
  31.  
  32.     foreach f $arglist {
  33.         message [file tail $f]
  34.         if {![catch {set fid [open $f]}]} {
  35.             scanfile $cid $fid
  36.             close $fid
  37.         }
  38.     }
  39.     scancontext delete $cid
  40.  
  41.     if {$opened} {
  42.         select [nextLineStart [nextLineStart 0]] [nextLineStart [nextLineStart [nextLineStart 0]]]
  43.         setWinInfo dirty 0
  44.         setWinInfo read-only 1
  45.     }
  46.     message ""
  47. }
  48.  
  49. proc grepfset {args} {
  50.     global fileSets
  51.  
  52.     set num [expr [llength $args] - 2]
  53.     set exp [lindex $args $num]
  54.     set fset [lindex $args [expr $num + 1]]
  55.     eval greplist [lrange $args 0 [expr $num-1]] {$exp $fileSets($fset)}
  56. }
  57.  
  58. proc grep {exp args} {
  59.     set files {}
  60.     foreach arg $args {
  61.         if {![catch {glob -t TEXT $arg} lst]} {
  62.             append files " " $lst
  63.         }
  64.         if {![catch {glob -t ttro $arg} lst]} {
  65.             append files " " $lst
  66.         }
  67.     }
  68.     if {$files==""} {return $lst}
  69.     set cid [scancontext create]
  70.     scanmatch $cid $exp {
  71.         if {!$blah} {
  72.             set blah 1
  73.             set lines "(<cr> to go to match)\r"
  74.         }
  75.         set l [expr 20 - [string length [file tail $f]]]
  76.         append lines "\"[file tail $f]\"[format "%$l\s" ""]; Line $matchInfo(linenum): $matchInfo(line)\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t∞$f\r"
  77.     }
  78.  
  79.     set blah 0
  80.     set lines ""
  81.  
  82.     foreach f $files {
  83.         if {![catch {set fid [open $f]}]} {
  84.             message [file tail $f]
  85.             scanfile $cid $fid
  86.             close $fid
  87.         }
  88.     }
  89.     scancontext delete $cid
  90.     return [string trimright $lines "\r"]
  91. }
  92.  
  93.